<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title> 用户个人信息</title>
    <link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">
    <style>
        #test{
            border: 2px solid black;
            text-align: center;
            /*transform: scale(0.9);*/
            margin-top: 0px;
            margin-bottom: 0px;
        }
        #test2{
            border: 2px solid black;
        }
        .quantity-section{
            margin-top: 50px;
            text-align: center;
        }
    </style>
</head>
<body>
<div id="app">
    <el-page-header @back="goBack" content="24°个人中心">
    </el-page-header>
    <div id="test">
        <h2>修改个人信息</h2>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="地址信息" prop="addr">
                <el-input v-model="ruleForm.addr"></el-input>
            </el-form-item>
            <el-form-item label="手机号码" prop="tele">
                <el-input v-model="ruleForm.tele"></el-input>
            </el-form-item>
            <el-form-item label="用户编号" prop="age">
                <el-input v-model.number="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
    <br>
    <div id="test2">
        <el-table :data="tableData" borderstyle="width: 100%">
            <el-table-column
                    prop="userId"
                    label="用户编号"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="userName"
                    label="姓名"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="userPassword"
                    label="密码">
            </el-table-column>
            <el-table-column
                    prop="userAddr"
                    label="地址">
            </el-table-column>
            <el-table-column
                    prop="usertel"
                    label="手机号码">
            </el-table-column>
            <el-table-column
                    prop="balance"
                    label="余额">
            </el-table-column>
        </el-table>
    </div>
    <div class="quantity-section">
        <el-button type="primary" @click="addBalance" size="medium">充值</el-button>
    </div>
</div>

</body>
<script src="../js/vue.js"></script>
<script src="../element-ui/lib/index.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    const urlParams = new URLSearchParams(window.location.search);
    const uname=urlParams.get('userName');
    var Main = {
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('编号不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value > 2000 || value< 1000) {
                            callback(new Error('请输入正确的编号'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var validateTele = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                }
                var reg = /^1[3|4|5|7|8][0-9]{9}$/;
                if (!reg.test(value)) {
                    callback(new Error('请输入正确的手机号'));
                } else {
                    callback();
                }
            };
            var validateAddr = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('地址不能为空'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    age: '',
                    tele:'',
                    addr:''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    age: [
                        { validator: checkAge, trigger: 'blur' }
                    ],
                    tele: [
                        { validator: validateTele, trigger: 'blur' }
                    ],
                    addr: [
                        { validator: validateAddr, trigger: 'blur' }
                    ]
                },
                tableData: [
                    {
                    userId: '2001',
                    userName: '朱王',
                    userPassword: '123456',
                    balance:'50',
                    userAddr: '湖南省常德市',
                    usertel: '15115902252'
                }
                ],
                value: new Date()
            };
        },
  /*      created(){
            this.admfind();
        },*/
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
/*                        alert(this.ruleForm.pass);
                        alert(this.ruleForm.age);
                        alert(this.ruleForm.addr);
                        alert(this.ruleForm.tele);*/
                        var pass=this.ruleForm.pass;
                        var usermid=this.ruleForm.age;
                        var addr=this.ruleForm.addr;
                        var tele=this.ruleForm.tele;
                        axios.get('../user',{params:{op:"usermodify",pass: pass,usermid: usermid,addr:addr,tele:tele,userName:uname}}).then(response => {
                            alert("修改成功！！！");

                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            userfind(){
                axios.get("../users?op=UsersInfoByName&userName="+uname).then(response=>{
                    console.log(response.data);
                    this.tableData = response.data.data;
/*                    alert(this.tableData);*/
                })
            },
            goBack() {
                console.log('go back');
                window.history.back(); // 返回上一个页面
            },
            addBalance(){
                this.$prompt('请输入充值金额', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /^[1-9]\d*$/,
                    inputErrorMessage: '输入金额格式不正确'
                }).then(({ value }) => {
                    const currentDate = new Date();
                    const options = {
                        timeZone: 'Asia/Shanghai',
                        hour12: false,
                        hour: 'numeric',
                        minute: 'numeric',
                        second: 'numeric'
                    };
                    const formattedChineseTime = currentDate.toLocaleString('zh-CN', options);
                    /*console.log(formattedDate+"+++++++++++++++++++");*/
                        this.$message({
                            type: 'success',
                            message: '您于'+formattedChineseTime+'成功充值了: ' + value + "元"
                        });
                        axios.get("../users?op=addBalance&balance="+value+"&userName="+uname).then(response=>{
                            console.log(response);
                        })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消充值'
                    });
                });
            }
        },
        mounted(){
            this.userfind();
        }
    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')
</script>
</html>